package com.library.utils
{
	public class NumberUtil
	{
		/**
		 * short cut a float number to a short digits format
		 * 
		 * @return a short-cutted number
		 */ 
		public static function formatNumber(number:Number,digit:uint=2):Number
		{
			var tensValue:uint=1;
			var digitNum:uint=digit;
			while(digitNum>0)
			{
				tensValue*=10;
				digitNum--;
			}
			return Math.round(number*tensValue)/tensValue;
		}
		
		/**
		 * note:(-4).toString(radix:int),will return a numeber in binary format as "-100".
		 * 
		 * @return a string of a number in binary
		 * @see Number.toString
		 */ 
		public static function toBinaryString(num:int):String
		{
			var resultStr:String="";
			
			if(num >= 0)
			{
				resultStr = num.toString(2);
				while(resultStr.length < 32)
				{
					resultStr = "0" + resultStr;
				}
			}
			else
			{
				var tempNum:int=num;
				var digitalLength:uint=32;
				var i:int=0;
			
				while(i<digitalLength)
				{
					resultStr=String(tempNum&0x01)+resultStr;
					tempNum=tempNum>>1;
					i++;
				}
			}
			return resultStr;
		}
		/**
		 * check the param orient is in the range between the lowLimite and the topLimte or not
		 * if the orientNumber is smaller than the lowLimite,return the lowLimite
		 * if the orientNumber is bigger than the topLimite,return the topLimite
		 */ 
		public static function qualifiedNumber(orientNumber:Number,lowLimite:Number,topLimite:Number):Number
		{
			var qualifiedValue:Number=orientNumber;
			if(qualifiedValue<lowLimite)
			{
				qualifiedValue=lowLimite;
			}
			else if(qualifiedValue>topLimite)
			{
				qualifiedValue=topLimite;
			}
			return qualifiedValue;
		}
		
		public static function random1(maxValue:Number, digit:uint=2):Number{
			var returnValue:Number = formatNumber( Math.random() * maxValue, digit);
			return returnValue;
		}
		
		public static function random2(value1:Number, value2:Number, digit:uint=2):Number{
			
			var minValue:Number = (value1 < value2) ? value1 : value2;
			var maxValue:Number = (value1 < value2) ? value2 : value1;
			var returnValue:Number = minValue + Math.random() * (maxValue - minValue);
			returnValue = formatNumber(returnValue, digit);
			return returnValue;
		}
		
		public static function degreeToRadian(degree:Number, digit:uint=2):Number
		{
			var returnValue:Number = Math.PI/180 * degree;			
			
			returnValue = formatNumber(returnValue, digit);
			return returnValue;
		}
	}
}